package com.show.antenna.tool;

public class ReceiveChangeMath {


    /**
     * 墨卡托变换转换成平面坐标
     */
    public double[] geo2xy_Mercator(double L, double B) {
        double a = 6378137;              //地球长轴
        double b = 6356752.31424518;     //地球短轴
        double e1 = Math.sqrt(POW(a, 2) - POW(b, 2) / POW(a, 2));  //第一离心率sqrt((a ^2-b^2) /a ^2);
        //double e2 = Math.sqrt(POW(a, 2) - POW(b, 2) / POW(b, 2));  //第二离心率sqrt((a ^2-b^2) /b ^2);
        double L0 = 118.6;
        double B0 = 24.9;

        L0 = L0 * Math.PI / 180;
        B0 = B0 * Math.PI / 180;
        L = L * Math.PI / 180;
        B = B * Math.PI / 180;

        //q=log(tan(pi/4+B/2).*((1-e1*sin(B))./(1+e1*sin(B))).^(e1/2));
        //k=(a*cos(B0))./sqrt(1-e1^2*(sin(B0))^2);
        double q = LOG(TAN(Math.PI / 4 + B / 2) * POW((1 - e1 * SIN(B)) / (1 + e1 * SIN(B)), e1 / 2));
        double k = (a * COS(B0)) / Math.sqrt(1 - POW(e1, 2) * POW(SIN(B0), 2));

        double x = k * q;
        double y = k * (L - L0);
        double[] latlon = new double[]{x, y};
        return latlon;
    }

    /**
     * 用户点所在方向与x轴正轴夹角theta_phi0_sample
     * 天线横坐标x_t，
     * 天线纵坐标y_t，
     * 用户点横坐标x_u，
     * 用户点纵坐标y_u
     */
    public double thetaPhi(double x_t, double y_t, double x_u, double y_u) {
        double theta_phi0_sample = 0.0;
        double user_x = x_u - x_t;
        double user_y = y_u - y_t;
        //第一象限
        if (user_x >= 0 && user_y >= 0) {
            theta_phi0_sample = ATAN(ABS(user_y) / ABS(user_x)) * 180 / Math.PI;
            theta_phi0_sample = theta_phi0_sample / 360 * 2 * Math.PI;
        }
        //第四象限
        else if (user_x >= 0 && user_y <= 0) {
            theta_phi0_sample = 360 - ATAN(ABS(user_y) / ABS(user_x)) * 180 / Math.PI;
            theta_phi0_sample = theta_phi0_sample / 360 * 2 * Math.PI;
        }
        //第三象限
        else if (user_x <= 0 && user_y <= 0) {
            theta_phi0_sample = 360 - ATAN(ABS(user_y) / ABS(user_x)) * 180 / Math.PI;
            theta_phi0_sample = theta_phi0_sample / 360 * 2 * Math.PI;
        }
        //第二象限
        else if (user_x <= 0 && user_y >= 0) {
            theta_phi0_sample = 360 - ATAN(ABS(user_y) / ABS(user_x)) * 180 / Math.PI;
            theta_phi0_sample = theta_phi0_sample / 360 * 2 * Math.PI;
        }
        return theta_phi0_sample;
    }


    /**
     * 计算调整后的接收功率
     * theta_phi0_sample 用户点所在方向与x轴正轴夹角
     * theta_before 天线调整前方位角，
     * theta_later  天线调整后方位角，
     * rsrp         天线调整前接受功率，
     * rsrp_cal     天线调整后接收功率
     */
    public double changeReceiveValue(double theta_phi0_sample, double theta_before, double theta_later, double rsrp) {
        double K1 = 0.4773;
        //计算天线方位角调整前增益GT
        //GT=abs(cos(K(1)*(theta_phi0_sample-theta_before/360*2*pi)));
        double GT = ABS(COS(K1 * (theta_phi0_sample - theta_before / 360 * 2 * Math.PI)));
        //计算天线方位角调整后增益GT_change
        //GT_change=abs(cos(K(1) *(theta_phi0_sample-theta_later/360*2*pi)));
        double GT_change = ABS(COS(K1 * (theta_phi0_sample - theta_later / 360 * 2 * Math.PI)));
        //计算天线调整后接收功率rsrp_cal
        double rsrp_cal = rsrp * GT_change / GT;
        return rsrp_cal;
    }

    private double POW(double a, double b) {
        return Math.pow(a, b);
    }

    private double SIN(double value) {
        return Math.sin(value);
    }

    private double COS(double value) {
        return Math.cos(value);
    }

    private double TAN(double value) {
        return Math.tan(value);
    }

    private double ATAN(double value) {
        return Math.atan(value);
    }

    private double ABS(double value) {
        return Math.abs(value);
    }

    private double LOG(double value) {
        return Math.log(value);
    }
}
